package com.tudouji.project.flow.service.impl;

import com.flow.service.pojo.BasePojo;
import com.flow.service.pojo.ResultParam;
import com.flow.service.pojo.result.FlowResult;
import com.flow.service.process.service.IFlowBaseService;
import com.flow.service.process.service.impl.FlowProcessProxy;
import com.tudouji.common.utils.SecurityUtils;
import com.tudouji.framework.web.domain.AjaxResult;
import com.tudouji.project.bill.domain.Sellform;
import com.tudouji.project.bill.mapper.RechargeMapper;
import com.tudouji.project.bill.mapper.SellformMapper;
import com.tudouji.project.bill.mapper.TransferapplyMapper;
import com.tudouji.project.customer.mapper.CustomerMapper;
import com.tudouji.project.farepay.mapper.FarepayApplyMapper;
import com.tudouji.project.flow.service.IShareService;
import com.tudouji.project.flow.utils.FlowProcessUtil;
import com.tudouji.project.harvest.mapper.FreightorderMapper;
import com.tudouji.project.harvest.mapper.ProcurementcontractMapper;
import com.tudouji.project.harvest.service.impl.BaseServiceFactory;
import com.tudouji.project.pay.mapper.PayinfoMapper;
import com.tudouji.project.produce.mapper.MaterialretapplyMapper;
import com.tudouji.project.produce.mapper.PickinglistMapper;
import com.tudouji.project.purchasebill.service.IPurchasebillService;
import com.tudouji.project.system.domain.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class ShareServiceImpl implements IShareService {

    @Autowired
    private FlowProcessUtil processUtil;
    @Autowired
    private FlowProcessProxy flowProcessProxy;

    @Autowired
    private IPurchasebillService purchasebillService;

    @Autowired
    private SellformMapper sellformMapper;
    @Autowired
    private RechargeMapper rechargeMapper;
    @Autowired
    private TransferapplyMapper transferapplyMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private PickinglistMapper pickinglistMapper;
    @Autowired
    private MaterialretapplyMapper materialretapplyMapper;
    @Autowired
    private FarepayApplyMapper farepayApplyMapper;
    @Autowired
    private ProcurementcontractMapper procurementcontractMapper;
    @Autowired
    private FreightorderMapper freightorderMapper;
    @Autowired
    private PayinfoMapper payinfoMapper;

    @Override
    public AjaxResult share(Long id, Long flowid, String userIds) {
        SysUser user= SecurityUtils.getLoginUser().getUser();
        BasePojo base=null;

        IFlowBaseService baseService= BaseServiceFactory.getFlowBaseServiceById(flowid);
        log.info("====Here we get baseService in Share :" + baseService);
        if (baseService!=null){
            base=baseService.getBaseById(id);
            base.prepareForProcess();
            FlowResult result = flowProcessProxy.share(flowid, userIds, user,base);
            if(result.getMsg().equals(ResultParam.SUCCESS)){
                return AjaxResult.success();
            }else{
                return AjaxResult.error();
            }
        }

        if(flowid.equals(26)){
            base = purchasebillService.selectPurchasebillById(id);
        }
        if(flowid.equals(28)){
            base = transferapplyMapper.selectTransferapplyByNewId(id);

        }
        if(flowid.equals(29)){
            base = customerMapper.selectCustomerByNewId(id);
        }
        if(flowid.equals(30)){
            base = pickinglistMapper.selectPickinglistByNewId(id);
        }
        if(flowid.equals(31)){
            base = materialretapplyMapper.selectMaterialretapplyByNewId(id);
        }
        if(flowid.equals(new Long(34)) || flowid.equals(new Long(35)) || flowid.equals(new Long(36))){
            Sellform sellform = sellformMapper.selectBaseSellformById(id);
            base=sellform;
            base.setCode(sellform.getSellcode());
        }
        if(flowid.equals(37)){
            base = rechargeMapper.selectRechargeByNewId(id);
        }
        if(flowid.equals(38)){
            base = farepayApplyMapper.selectFarepayApplyById(String.valueOf(id));
        }
        if(flowid.equals(39)){
            base = procurementcontractMapper.selectProcurementcontractByNewId(id);
        }
        if(flowid.equals(40)){
            base = freightorderMapper.selectFreightorderByAutoId(id);
        }
        if(flowid.equals(41)){
            base = payinfoMapper.selectPayinfoByAutoId(id);
        }
        if(flowid.equals(42)){
            base = payinfoMapper.selectPayinfoByAutoId(id);
        }
        FlowResult result = flowProcessProxy.share(flowid, userIds, user,base);
        if(result.getMsg().equals(ResultParam.SUCCESS)){
            return AjaxResult.success();
        }else{
            return AjaxResult.error();
        }
    }
}
